﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import flash.utils.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class BabyMediator extends AbstractActorMediator {

        private var _cryTimer:Timer;

        override public function onRegister():void{
            super.onRegister();
            this._cryTimer = new Timer(1000, 1);
            this._cryTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.cryTimerComplete);
            eventMap.mapListener(eventDispatcher, EventConstants.SET_BABY_TREATMENT, this.parseTreatmentStates);
            eventMap.mapListener(eventDispatcher, EventConstants.FINALIZE_BABY_TREATMENT, this.finalizeTreatment);
            eventMap.mapListener(eventDispatcher, EventConstants.BOOST_MACHINE, this.onBoost);
            eventMap.mapListener(eventDispatcher, EventConstants.VISIT_FRIEND, this.onVisitFriend);
            eventMap.mapListener(eventDispatcher, EventConstants.BUBBLE_CLOSED, this.checkSpeechbubble);
            eventMap.mapListener(this.view, EventConstants.ON_ANIMATION_FINISHED, this.parseTreatmentStates);
            this.view.setAnimationState(this.vo.animationState);
            this.parseTreatmentStates();
        }
        private function checkSpeechbubble(_arg1:Event):void{
            if (this.vo){
                this.vo.checkSpeechbubble();
            };
        }
        override protected function setPosition(_arg1:Number, _arg2:Number):void{
            this.view.direction = ((_arg1)>=this.view.x) ? -1 : 1;
            super.setPosition(_arg1, _arg2);
        }
        override protected function startMoving():void{
            if (this.vo.canWalk){
                super.startMoving();
            };
        }
        override protected function updateActor(_arg1:Object):void{
            super.updateActor(_arg1);
        }
        override protected function onClick(_arg1:MouseEvent):void{
            if (this.view.animationState != GameConstants.ANIMATION_STATE_WALKING){
                dispatch(new InfoEvent(EventConstants.CLICK_ON_BABY, this.vo.id, this.view));
            };
        }
        private function parseTreatmentStates(_arg1:InfoEvent=null):void{
            if (this.vo.hasTreatmentsFinished){
                this.handleTreatmentsFinished();
            } else {
                switch (this.vo.currentTreatment.state){
                    case GameConstants.BABY_STATE_NEEDED:
                        this.handleNeeded();
                        break;
                    case GameConstants.BABY_STATE_FINALIZED:
                        this.handleFinalized();
                        break;
                    case GameConstants.BABY_STATE_STARTED:
                        this.handleStarted();
                        break;
                };
            };
            if (this.vo.isBlocked){
                this.setAnimationSad();
            };
        }
        private function handleTreatmentsFinished():void{
            this.vo.state = WorldConstants.CURED;
            this.setAnimationHappy();
        }
        private function onBoost(_arg1:InfoEvent):void{
            if (((this.vo.currentTreatment) && ((_arg1.id == this.vo.currentTreatment.id)))){
                this.setAnimationWake();
            };
        }
        private function handleNeeded():void{
            this.setAnimationSad();
        }
        private function handleStarted():void{
            if (!this.vo.isBlocked){
                this.setAnimationSleep();
            };
        }
        private function handleNeedsHug():void{
            this.setAnimationSad();
        }
        private function handleFinalized():void{
            this.setAnimationWalk();
        }
        private function finalizeTreatment(_arg1:InfoEvent=null):void{
            if ((((userModel.mySocialID == userModel.visitingFriendID)) && ((_arg1.id == this.vo.id)))){
                this.updateBabyTreatment(this.vo, WorldConstants.FINALIZE_BABY_TREATMENT, this.onTreatmentFinalized);
            };
        }
        private function onTreatmentFinalized(_arg1:ResultVO):void{
            var _local2:Object;
            if (_arg1.success){
                _local2 = _arg1.data.finalize_baby_treatment;
                this.vo.updateCurrentTreatment(_local2.treatment);
                this.parseTreatmentStates();
            };
        }
        private function updateBabyTreatment(_arg1:IBaby, _arg2:String, _arg3:Function, _arg4:Object=null):void{
            var _local5:UpdateTreatmentEvent = new UpdateTreatmentEvent(EventConstants.UPDATE_BABY_TREATMENT, _arg1, _arg2, _arg3, _arg4);
            dispatch(_local5);
        }
        private function setAnimationWalk():void{
            this._cryTimer.stop();
            this.view.setAnimationState(GameConstants.ANIMATION_STATE_WALKING, true);
        }
        private function setAnimationWake():void{
            this._cryTimer.stop();
            if (this.view.animationState == GameConstants.ANIMATION_STATE_SLEEP){
                this.view.setAnimationState(GameConstants.ANIMATION_STATE_WAKE, true);
                setTimeout(this.parseTreatmentStates, 3000);
            };
        }
        private function setAnimationSleep():void{
            this._cryTimer.stop();
            this.view.setAnimationState(GameConstants.ANIMATION_STATE_SLEEP, true);
            this.vo.removeSpeechBubble();
        }
        private function setAnimationHappy():void{
            this._cryTimer.stop();
            this.view.setAnimationState(GameConstants.ANIMATION_STATE_HAPPY, true);
        }
        private function setAnimationSad():void{
            this._cryTimer.stop();
            this.view.setAnimationState(GameConstants.ANIMATION_STATE_STILL, true);
            var _local1:int = Numbers.randomIntBetween(10000, 30000);
            this._cryTimer.delay = _local1;
            this._cryTimer.start();
        }
        private function cryTimerComplete(_arg1:TimerEvent):void{
            if (this.view){
                this.view.setAnimationState(GameConstants.ANIMATION_STATE_SAD, true);
                setTimeout(this.finishCrying, 4000);
            };
        }
        private function finishCrying():void{
            if (((this.view) && ((this.view.animationState == GameConstants.ANIMATION_STATE_SAD)))){
                this.setAnimationSad();
            };
        }
        private function get view():BabyView{
            return ((viewComponent as BabyView));
        }
        private function get vo():BabyVO{
            return ((_vo as BabyVO));
        }
        override protected function toggleFacade(_arg1:BuildingEvent=null):void{
            var _local2:Boolean = (this.vo.isVisible) ? !(gameStateModel.isFacadeVisible) : false;
            if (_local2){
                this.view.show();
            } else {
                this.view.hide();
            };
        }
        private function onVisitFriend(_arg1:Event):void{
            this._cryTimer.stop();
        }

    }
}//package net.wooga.pets.views.world 
